package cn.zzuisa.controller;


import cn.zzuisa.config.TokenManager;
import cn.zzuisa.entity.Access;
import cn.zzuisa.service.AccessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import cn.zzuisa.base.R;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author frank
 * @since 2019-05-11
 */
@Api(tags = "访客管理")
@RestController
@RequestMapping("/access")
public class AccessController {


    @Autowired
    private AccessService accessService;
    @ApiOperation("userId不传看自己的")
    @GetMapping("list")
    public R<List> list(HttpServletRequest request,Long userId) {
        userId = userId == null ? TokenManager.get(request.getHeader("token")) : userId;
        List<Access> list =
                accessService.list(new QueryWrapper<Access>().eq("access_id",userId).orderByDesc("access_time").groupBy("access_id"));
        if(list != null && list.size() > 11) {
            // 根据name去重
            list = list.subList(0,list.size() > 10 ? 10 : list.size());
        }
        return R.ok(list);

    }


}
